package com.weme.mine.activity

import android.content.Context
import android.os.Bundle
import android.util.Log
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import com.alibaba.android.arouter.facade.annotation.Route
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.QuickViewHolder
import com.gyf.immersionbar.BarHide
import com.gyf.immersionbar.ImmersionBar
import com.jeremyliao.liveeventbus.LiveEventBus
import com.makeramen.roundedimageview.RoundedImageView
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener
import com.weme.common.arouter.ARouterPaths
import com.weme.common.base.BaseMvvmActivity
import com.weme.common.util.*
import com.weme.common.util.LiveConfig.NET_WORK_BLACK
import com.weme.common.view.SmoothImageView
import com.weme.common.view.SmoothRoundedRectView
import com.weme.mine.R
import com.weme.mine.databinding.ActivityBlockBinding
import com.weme.mine.viewmode.MineViewMode
import dev.kratos.api.user_server.v1.GetBlackListReply
import dev.kratos.api.user_server.v1.UnBlackReply
import dev.kratos.api.user_server.v1.UnBlackRequest
import dev.kratos.api.user_server.v1.Users
import io.agora.CallBack
import io.agora.chat.ChatClient

@Route(path = ARouterPaths.MINE_BLOCK_ACTIVITY)
class BlockActivity : BaseMvvmActivity<ActivityBlockBinding, MineViewMode>() {
    private var pageIndex: Int = 1
    private var allHistoryList = mutableListOf<Users>()

    override fun initView(savedInstanceState: Bundle?) {
        ImmersionBar.with(this).hideBar(BarHide.FLAG_HIDE_NAVIGATION_BAR).titleBar(mBinding.customTopNavigationBar).init()
        mBinding.rcvList.setVerticalLayoutManager()
        mBinding.rcvList.adapter = histortAdapter
        histortAdapter.isStateViewEnable = true
        val view = View.inflate(this, com.weme.common.R.layout.layout_empty_state, null)
        view.findViewById<TextView>(com.weme.common.R.id.btRefresh).gone()
        view.findViewById<TextView>(com.weme.common.R.id.tv_empty).text = getString(com.example.resources.R.string.block_empty_txt)

        histortAdapter.stateView = view //
        histortAdapter.submitList(allHistoryList)
        mBinding.smartLayout.setOnRefreshLoadMoreListener(object : OnRefreshLoadMoreListener {
            override fun onRefresh(refreshLayout: RefreshLayout) {
                pageIndex = 1
                viewModel.getBlockList(pageIndex)

            }

            override fun onLoadMore(refreshLayout: RefreshLayout) {
                pageIndex++
                viewModel.getBlockList(pageIndex)
            }
        })
     LiveEventBus.get<Any>(LiveConfig.NET_WORK_CHAT).observe(this){
            if(it is UnBlackReply){
                if(removeUser!=null){
                    histortAdapter.remove(removeUser!!)
                    showToast(getString(com.example.resources.R.string.block_unbolck_tip,removeUser!!.nickname))

                }

            }
        }
     LiveEventBus.get<Any>(NET_WORK_BLACK).observe(this){
            if(it is GetBlackListReply){
                if(pageIndex == 1){
                    histortAdapter.isStateViewEnable = true
                    allHistoryList.clear()
                    allHistoryList.addAll(it.usersList)
                    histortAdapter.notifyDataSetChanged()
                    mBinding.smartLayout.finishRefresh()
                    mBinding.smartLayout.finishLoadMore()
                }else{
                    allHistoryList.addAll(it.usersList)
                    histortAdapter.notifyDataSetChanged()
                    mBinding.smartLayout.finishRefresh()
                    mBinding.smartLayout.finishLoadMore()
                }

            }
        }
    }

    override fun onResume() {
        super.onResume()
        mBinding.smartLayout.autoRefresh()
    }

    var removeUser :Users? = null
    private val histortAdapter = object : BaseQuickAdapter<Users, QuickViewHolder>() {
        override fun onBindViewHolder(holder: QuickViewHolder, position: Int, item: Users?) {
            val rimHead = holder.getView<RoundedImageView>(R.id.rimHead)
            val vCircle = holder.getView<View>(R.id.vCircle)
            val imCountry = holder.getView<SmoothImageView>(R.id.imCountry)
            val tvCountry = holder.getView<TextView>(R.id.tvCountry)
            val smvUnblock = holder.getView<TextView>(R.id.smvUnblock)
            
            item?.apply {
                rimHead.loadImage(this.headPic)
                holder.setText(R.id.tvName, setNameAge((this.nickname + "," + this.age)))
                val country = getCountryBean(this.countryCode)
                country?.let {
                    Log.e("tag", it.countryFlag.toString())
                    imCountry.loadImage(it.countryFlag.toString())
                    tvCountry.text = it.countryName
                }

                when (this.onlineStatus) {
                    1 -> {
                        vCircle.visible()
                        vCircle.setBackgroundResource(com.example.resources.R.drawable.online_dot)
                    }
                    2 -> {
                        vCircle.visible()
                        vCircle.setBackgroundResource(com.example.resources.R.drawable.busy_dot)
                    }
                    else -> {
                        vCircle.gone()

                    }
                }
                smvUnblock.setOnClickListener {
                    removeUser = this
                    viewModel.setUnBlock(UnBlackRequest.newBuilder().setBlackUid(this.uid).build())
                }
                
            }




        }

        override fun onCreateViewHolder(context: Context, parent: ViewGroup, viewType: Int): QuickViewHolder {
            return QuickViewHolder(R.layout.item_block, parent)
        }


    }

    override fun getViewModelClass(): Class<MineViewMode> = MineViewMode::class.java


}